package com.cloudserver.springboot.service.impl;

import com.cloudserver.springboot.Constants.LoginInfo;
import com.cloudserver.springboot.bean.User;
import com.cloudserver.springboot.bean.UserExample;
import com.cloudserver.springboot.dao.UserCharacterMapper;
import com.cloudserver.springboot.dao.UserMapper;
import com.cloudserver.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zqq
 * @create 2020-10-06 14:55
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserCharacterMapper userCharacterMapper;



    /**
     * @Description: loginservice 处理登录，判断密码是否正确
     * @param: [user] 包含了账户和密码两个参数
     * @return: java.lang.Integer   users.get(0).getId()   表示账户密码正确，返回相应的id
     *                              PASSWORD_ERROR_CODE 表示密码错误
     *                              ACCOUNT_NOT_EXIT_CODE 表示账户不存在
     * @auther: zqq
     * @date: 20/10/6 15:05
     */
    @Override
    public Integer loginservice(User user) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andAccountEqualTo(user.getAccount());
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() != 0){
            if (users.get(0).getPassword().equals(user.getPassword())){
                return users.get(0).getId();
            }else {
                return LoginInfo.PASSWORD_ERROR_CODE;
            }
        }else {
            return LoginInfo.ACCOUNT_NOT_EXIT_CODE;
        }
    }

    @Override
    public boolean addUser(User user) {
        User user1 = new User();
        user1.setAccount(user.getAccount());
        user1.setPassword(user.getPassword());
        user1.setUsername(user.getUsername());
        user1.setTelephone(user.getTelephone());
        user1.setEmail(user.getEmail());
        user1.setUserDescription(user.getUserDescription());
        int addaccount = userMapper.insert(user1);

        if(addaccount == 1){
            return true;
        }else {
            return false;
        }
    }
}
